﻿using Autofac;
using System;
using System.Linq;
using System.Reflection;
using System.Web.Compilation;
using Zj.HttpCodeDemo.Bus.Simple;
using Zj.HttpCodeDemo.Domain.Events;
using Zj.HttpCodeDemo.EventStore.Dapper;

namespace Zj.HttpCodeDemo.Mvc
{
    public class AutofacExt
    {
        public readonly static ContainerBuilder _builder;
        static  AutofacExt()
        {
            _builder = new ContainerBuilder();
        }
        public static void Initialize()
        {
            var assemblies = BuildManager.GetReferencedAssemblies().Cast<Assembly>()
                   .ToArray();
            assemblies = assemblies.Where(lib => lib.FullName.EndsWith("Repository", StringComparison.OrdinalIgnoreCase))
                .ToArray();

            _builder.RegisterAssemblyTypes(assemblies);
            _builder.RegisterType(typeof(DapperEventStore)).As(typeof(IEventStore));
            _builder.RegisterType(typeof(IEventExcutionContext)).As(typeof(IEventExcutionContext));
            //_builder.RegisterType(new SimpleEventBus(_builder.Build().Resolve<IEventExcutionContext>()));

            _builder.RegisterType(typeof(SimpleEventBus)).As(typeof(IEventBus));
        }
    }
}